import cv2
import numpy as np
# 形态学基础->腐蚀and膨胀
# 腐蚀：去噪音以及元素分割并计数
o = cv2.imread('erode.bmp',cv2.IMREAD_UNCHANGED)

kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
erode1 = cv2.erode(o,kernel1)

kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT,(9,9))
erode2 = cv2.erode(o,kernel2)

cv2.imshow('orignal',o)
cv2.imshow('erode1',erode1)
cv2.imshow('erode2',erode2)


# 膨胀：和腐蚀效果相反，用于扩展图像的边界
o2 = cv2.imread('dilation.bmp')

kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
dilation1 = cv2.dilate(o2,kernel3)
dilation2 = cv2.dilate(o2,kernel3,iterations=9)
cv2.imshow('orignal2',o2)
cv2.imshow('dilation1',dilation1)
cv2.imshow('dilation2',dilation2)

cv2.waitKey()
cv2.destroyAllWindows()
